/*
 * Main.fx
 *
 * Created on 20-nov-2009, 10:04:51
 */

package javafxproject;

import javafx.stage.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.text.*;

import javafx.scene.chart.BarChart;
import javafx.scene.chart.part.CategoryAxis;
import javafx.scene.chart.part.NumberAxis;

import javafx.util.Sequences;


/**
 * @author Ignace
 */

//vaste layout patroon
def height = 200;
def width = 200;
def spaceBetweenRadioButtons = 15;
//stage2 is voor de grafiek!!!
var stage2;

//variabele voor de meerkeuze mogelijkheden
var aantal1 = 0;
var aantal2 = 0;
var aantal3 = 0;


var question = Label {
    layoutX : 0
    layoutY : 0
    text: "Wat vind je van JavaFX?"
    font: Font {
            name: "Verdana"
            size: 14
        }


}
//radioGroep voor de radiobuttons
def radioButtonGroep = ToggleGroup{};

var rButton1 = RadioButton {
        toggleGroup: radioButtonGroep
        layoutX: 0
        layoutY: question.layoutY + 20
        text: "Leuk"

}

var rButton2 = RadioButton {
        toggleGroup: radioButtonGroep
        layoutX: rButton1.layoutX
        layoutY: rButton1.layoutY + spaceBetweenRadioButtons
        text: "Stom"
}

var rButton3 = RadioButton {
        toggleGroup : radioButtonGroep
        layoutX: rButton2.layoutX
        layoutY: rButton2.layoutY + spaceBetweenRadioButtons
        text : "Maakt me niet uit"
}


//MET DANK AAN http://java.sun.com/developer/technicalArticles/javafx/v1_2_charts/
/**
 * Finds the highest value between both lastPrice and newPrice elements
 */
function getUpperBound():Number {
  var maxValues = [];
  insert Sequences.max(aantalArrayBarChart) into maxValues;
  var highestValue = Sequences.max(maxValues as Number[]);
  println("Highest Value = {highestValue}");
  return highestValue as Number
}

/**
 * Categories for the bar chart
 */
def categories:String[] = ["Keuze: Zie legende"];

/**
 * A list of stock symbols for use in the graphs
 */
def stocks:String[] = ["{rButton1.text}","{rButton2.text}","{rButton3.text}"];

/**
 * Simultes the new prices from a RSSTask
 */
var aantalArrayBarChart:Number[];
//var aantalArrayBarChart:Number [];

var barChart:BarChart;

var knop = Button {
        layoutX: 10
        layoutY: rButton3.layoutY + (spaceBetweenRadioButtons*2)
        text: "Stem!"
        action: function() {
                var output:String = "Resultaat:\n";
                var inOrde = true;
                if(rButton1.selected) {
                        aantal1++;
                } else if(rButton2.selected) {
                        aantal2++;
                } else if(rButton3.selected) {
                        aantal3++;
                } else {
                    inOrde = false;
                    Alert.inform("Gelieve een keuze te maken");
                }

                if(inOrde == true) {                        
                        aantalArrayBarChart = [{aantal1},{aantal2},{aantal3}];
                        //MET DANK AAN http://java.sun.com/developer/technicalArticles/javafx/v1_2_charts/
                        barChart = BarChart {
                            title: "Wat vinden de gebruikers van JavaFX:"
                            titleFont: Font { size: 24 }
                            categoryGap: 25
                            barGap: 10
                            categoryAxis: CategoryAxis {
                                categories: categories
                            }
                            valueAxis: NumberAxis {
                                label: "Aantal stemmen"
                                lowerBound: 0
                                upperBound: getUpperBound() + 5
                                tickUnit: 5
                            }
                            data: for (s in stocks) {
                                BarChart.Series {
                                name: s
                                data: [
                                    BarChart.Data {
                                        category: categories[1]
                                        value: aantalArrayBarChart[indexof s]
                                    }
                                ]
                                }
                            }
                        };

                        stage2 = Stage {
                            title: "Grafiek"
                            visible: true
                            scene : Scene {
                                content: [barChart]
                            }
                        }
                }
        }
}


var scene = Scene {
    width : width
    height: height
    content: [question, knop, rButton1, rButton2, rButton3]
}

var stage = Stage {
    title: "JavaFx - Project RadioButtons"
    scene : scene
}
